﻿@page "/entries/birthdaycalendar"
@page "/birthday"

@inject NavigationManager NavigationManager
@inject IDataCacheService _dataCacheService
@inject IHttpService _httpService


<CnGalWebSite.Shared.MasaComponent.Shared.Cards.TitleTip Title="角色生日日历" Description="可以在这里按月份查看所有角色的生日" />
<MDCard Class="pa-4 mb-4">
    <div class="title-large" style=" text-align: center; ">🎂 角色生日日历</div>
</MDCard>
<CnGalWebSite.Shared.MasaComponent.Shared.Components.Calendars.MasaCalendar Month="Date" MonthChanged="MonthChanged">
    <div style=" display: flex; flex-direction: column;" class="h-100">
        <div class="@((context.Date==DateTime.Now.ToCstTime().Date?"fw-bold ":"")+" label-large pa-2")">
            @context.Day.ToString()
        </div>
        <div style="display: flex; flex-wrap: wrap; justify-content: flex-end; align-content: flex-end; " class="pa-1 h-100">
            @foreach (var item in Model.Where(s => s.Brithday.Day == context.Day && s.Brithday.Month == context.Month))
            {
                <div class="pa-1">
                    <CnGalWebSite.Shared.MasaComponent.Shared.Cards.Entries.Brithdays.RoleButton Model="item" />
                </div>
            }
        </div>
    </div>
</CnGalWebSite.Shared.MasaComponent.Shared.Components.Calendars.MasaCalendar>

@code {
    DateTime Date = DateTime.Now.ToCstTime();
    List<RoleBrithdayViewModel> Model = new List<RoleBrithdayViewModel>();

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    [SupplyParameterFromQuery]
    [Parameter]
    public int Year { get; set; }
    [SupplyParameterFromQuery]
    [Parameter]
    public int Day { get; set; }
    [SupplyParameterFromQuery]
    [Parameter]
    public int Month { get; set; }

    protected override async Task OnInitializedAsync()
    {
        NavigationManager.LocationChanged -= LocationChanged;
        NavigationManager.LocationChanged += LocationChanged;

        LocationChanged(null, null);
    }

    public async void LocationChanged(object sender, LocationChangedEventArgs args)
    {
        if (NavigationManager.Uri.Contains("/birthday"))
        {
            if (Day != 0 && Month != 0 && Year != 0)
            {
                Date = new DateTime(Year, Month, Day, 0, 0, 0, DateTimeKind.Local);

            }
            else
            {
                Date = DateTime.Now.ToCstTime();
            }
            try
            {
                Model.Clear();
                Model.AddRange(await _dataCacheService.RoleBrithdaysDataCatche.GetCache($"?month={Date.Month}"));
                Model.AddRange(await _dataCacheService.RoleBrithdaysDataCatche.GetCache($"?month={Date.AddMonths(1).Month}"));
                Model.AddRange(await _dataCacheService.RoleBrithdaysDataCatche.GetCache($"?month={Date.AddMonths(-1).Month}"));
                StateHasChanged();
            }
            catch (Exception ex)
            {
                await ErrorHandler.ProcessError(ex, "获取角色生日失败");
            }
        }

    }


    public void MonthChanged(DateTime month)
    {
        if (Date.Date == month.Date)
        {
            
        }
        NavigationManager.NavigateTo($"/birthday?Year={month.Year}&Month={month.Month}&Day={month.Day}");
        

    }

}
